#!/bin/sh
#抽取日表数据到dwd_gas_open_toque_pdi表中
CUR_DIR=$(cd "$(dirname "${BASH_SOURCE-$0}")"; pwd)
UP_DIR=$(dirname $(dirname $(dirname "$PWD")))
echo ${UP_DIR}
source ${UP_DIR}/system_env.sh


START_TIME=$(date +%s)
hive_db=${hive_db}

if [ -z "${hive_db}" ];then
  hive_db=dataplatform_trip
else
  hive_db=${hive_db}
fi


if [ -z "$1" ];then
  part_time=$(date -d "1 days ago" +%Y%m%d)
else
  part_time=$1
fi

sql_suffix=$(cat <<EOF
 insert overwrite table ${hive_db}.dwd_gas_open_toque_pdi partition (day)
   SELECT tid ,
   sum(trip_mileage) as trip_mileage,
   sum(trip_fuel) as trip_fuel,
   sum(gas_open_percent0_mileage ),
   sum(gas_open_percent10_mileage),
   sum(gas_open_percent20_mileage),
   sum(gas_open_percent30_mileage),
   sum(gas_open_percent40_mileage),
   sum(gas_open_percent50_mileage),
   sum(gas_open_percent60_mileage),
   sum(gas_open_percent70_mileage),
   sum(gas_open_percent80_mileage),
   sum(gas_open_percent90_mileage),
   sum(gas_open_percent99_mileage),
   sum(gas_open_percent100_mileage),
   sum(gas_open_percent0_fuel    ),
   sum(gas_open_percent10_fuel   ),
   sum(gas_open_percent20_fuel   ),
   sum(gas_open_percent30_fuel   ),
   sum(gas_open_percent40_fuel   ),
   sum(gas_open_percent50_fuel   ),
   sum(gas_open_percent60_fuel   ),
   sum(gas_open_percent70_fuel   ),
   sum(gas_open_percent80_fuel   ),
   sum(gas_open_percent90_fuel   ),
   sum(gas_open_percent99_fuel   ),
   sum(gas_open_percent100_fuel  ),
   sum(torque0_mileage           ),
   sum(torque10_mileage          ),
   sum(torque20_mileage          ),
   sum(torque30_mileage          ),
   sum(torque40_mileage          ),
   sum(torque50_mileage          ),
   sum(torque60_mileage          ),
   sum(torque70_mileage          ),
   sum(torque80_mileage          ),
   sum(torque90_mileage          ),
   sum(torque99_mileage          ),
   sum(torque100_mileage         ),
   sum(torque0_fuel              ),
   sum(torque10_fuel             ),
   sum(torque20_fuel             ),
   sum(torque30_fuel             ),
   sum(torque40_fuel             ),
   sum(torque50_fuel             ),
   sum(torque60_fuel             ),
   sum(torque70_fuel             ),
   sum(torque80_fuel             ),
   sum(torque90_fuel             ),
   sum(torque99_fuel             ),
   sum(torque100_fuel            ),
   day
   from ${hive_db}.trip_statistics
   where day = ${part_time}
   group by tid,day;
EOF
)

CUR_DIR=$(cd "$(dirname "$0")"; pwd)
hive_config=$(cat  "${CUR_DIR}"/hive.config)
sql="${hive_config}${sql_suffix}"
echo "导入dwd_gas_open_toque_pdi的SQL为：${sql}"

#执行语句·
hive -e "${sql}"
if [ $? -ne 0 ];then
  echo "导入dwd_gas_open_toque_pdi失败，耗时$((`date +%s`-START_TIME))秒..."
else
  echo "导入dwd_gas_open_toque_pdi成功，耗时$((`date +%s`-START_TIME))秒..."
fi